package com.baidu.servlet;

import com.baidu.entity.AirBean;
import com.baidu.entity.DistrictBean;
import com.baidu.entity.MenuBean;
import com.baidu.service.AirService;
import com.baidu.service.MenuService;
import com.baidu.utils.Page;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 创作时间：2019/9/19 8:41
 * 作者：李增强
 */
@WebServlet("/airServlet")
public class AirServlet extends HttpServlet {
    AirService as = new AirService();

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("list".equals(method)){
            getAirList(req,resp);
        }else if("toupdate".equals(method)){
            toUpdateAir(req,resp);
        }else if("delete".equals(method)){
            deleteAirById(req,resp);
        }else if("update".equals(method)){
            updateAir(req,resp);
        }
    }
    protected void updateAir(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String districtid = req.getParameter("districtid");
        String monitortime = req.getParameter("monitortime");
        String pm10 = req.getParameter("pm10");
        String pm25 = req.getParameter("pm25");
        String monitoringstation = req.getParameter("monitoringstation");
        as.updateAir(districtid,monitortime,pm10,pm25,monitoringstation,id);
        resp.sendRedirect("airServlet?method=list");

    }
    protected void deleteAirById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        boolean flag = as.deleteAirById(id);
        if(flag){
            /**
             * 删除成功后，去list列表，因为我们直接调用的方法，把req传递过去了，req里面有我们刚刚删除时候的id，
             * 这个id又是，区域的id，一样，所以到了list中，这个id有值，按照区域查询去了
             * 解决方案，1、重定向到list
             * 转发的时候，把id情况
             */
            resp.sendRedirect("airServlet?method=list");
        }else{
            req.setAttribute("msg", "删除失败!");
            toUpdateAir(req, resp);
        }
    }
    protected void toUpdateAir(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        AirBean ab = as.getAirById(id);
        List<DistrictBean> list = as.getDistrictList();
        req.setAttribute("ab", ab);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/WEB-INF/pages/air/air_update.jsp").forward(req, resp);
    }

    protected void getAirList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String yema = req.getParameter("yema");
        int count= as.getAirCount(id);
        Page page = new Page(yema, count, "3");
        List<AirBean> list = as.getAirList(page,id);
        List<DistrictBean> dlist = as.getDistrictList();
        req.setAttribute("dlist", dlist);
        req.setAttribute("id", id);
        req.setAttribute("page", page);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/WEB-INF/pages/air/air_list.jsp").forward(req, resp);
    }
}
